<?php
require 'Model.php';

	class UserModel extends Model{
		function __Construct(){
		parent::__Construct();

	}
	function getUserList(){
		$sql="select * from users";//写sql语句
		$data=$this->conn->query($sql);//执行sql语句得到数据
		
		return $data;
	}
	function delUser($uid){


	$sql = "select * from users where uid=$uid";
	$result=mysql_query($sql);
	$row=mysql_fetch_assoc($result);
	$filename='./photos/'.$row["pic"];
	//echo $filename;
	//删除数据库相应信息，如果删除成功，删除该用户的头像文件
	$sql = "delete from users where uid=$uid";
	$result = mysql_query($sql) or die('删除失败！'.mysql_error());
	if(!mysql_error()){
		//如果有头像，则删除头像文件
		if($filename!='./photos/' && file_exists($filename))
			unlink($filename);
			return $result;
			/*$sql="delete from users where uid=$uid";
			$result=$this->conn->query($sql);
			return $result;*/

	}
		}
	
	

	function addUser($data,$file){
		
		/*//把数据插入数据库
	
             $uname=$_POST['username'];
             $upassword=$_POST['password'];
             $gender=$_POST['gender'];
             $birthdate=$_POST['birthdate'];
             $hobby=implode($_POST['hobby'],',');
             $degree=$_POST['degree'];
             $intro=$_POST['intro'];
			if($_FILES["pic"]['error']>0){
    
    		switch ($_FILES["pic"]["error"]) {
       			 case 3:echo "文件上传失败";
           		 break;
           		 case 4:echo "没有文件上传";
                break;
        default:echo "未知错误";
            
    }
    exit;   
}
	$allowtype=array("jpg","jpeg","png","PNG","gif","JPG","Bmp","flv");
	$arr=explode(".",$_FILES["pic"]["name"]);
	$suffix=$arr[count($arr)-1];
	//echo "文件类型：".$suffix;
	//echo "<br />";
	if (!in_array($suffix, $allowtype)) {
    echo "文件类型不支持！";
    }
    else{
	$filepath="photos/";
 	$newname=date("YmdHis").rand(100,999).".".$suffix;
	move_uploaded_file($_FILES["pic"]['tmp_name'], $filepath.$newname);

 	$sql="insert into users VALUES ('null','$uname','$upassword','$gender','$birthdate','$hobby','$degree','$intro','$newname')";
  	//echo $sql;
 	$result=$this->conn->query($sql);
        return $result;
        }*/

        //上传头像
        /*$arr=explode('.', $file['pic']['name']);
        $suffix=$arr[count($arr)-1];
        $newname=date("YmdHis").rand(100,999).'.'.$suffix;
        $filepath = './photos/';
        move_uploaded_file($_FILES['pic']['tmp_name'], $filepath.$newname);*/
        //上传文件到指定文件夹，tmp_name是file里带的

        $newname = $this->uploadfile($file);//调用uploadfile方法，得到newname

        $uname=$data['username'];
        $upassword=$data['password'];
        $gender=$data['gender'];
        $birthdate=$data['birthdate'];
        $hobby=implode($data['hobby'],',');
        $degree=$data['degree'];
        $intro=$data['intro'];
        $pic=$newname;
        $sql="insert into users VALUES ('null','$uname',md5('$upassword'),'$gender','$birthdate','$hobby','$degree','$intro','$pic')";
        $result=$this->conn->query($sql);
        return $result;
    }
	
	function edituser($uid){
		$sql = "select * from users where uid=$uid";
		//echo $sql;
		$data=$this->conn->query($sql);
		return $data;
		//var_dump($row);
		/*$uname = $row['uname'];
		$password = $row['upassword'];
		$gender = $row['gender'];
		$birthdate = $row['birthdate'];
		$hobby = $row['hobby'];
		$degree = $row['degree'];
		$intro = $row['intro'];
		$filepath = './photos/';
		$pic = UserPhotoPath.$row['pic'];
*/}
		function updateuser($data,$file){
			//接受数据
				$uid=$data['uid'];
				$username=$data['username'];
				$gender=$data['gender'];
				$birthdate=$data['birthdate'];
				$hobby=implode($data['hobby'], ',');
				$degree=$data['degree'];
				$intro=$data['intro'];
				$oldpic=$data['oldpic'];
			//如果修改了头像，需要上传新头像，否则原头像不动
				if ($file['pic']['size']==0) {
					$sql="update users set uname='$username',birthdate='$birthdate',gender=$gender,hobby='$hobby',degree=$degree,intro='$intro' where uid=$uid";
					//整型不用写单引号 degree=$degree
				}
				else{
					//删除原来的头像
					//如果有头像，则删除头像文件
					if($filename!='./photos/' && file_exists($filename))
					{$filename='./photos/'.$oldpic;
					unlink($filename);}
					//调用方法上传图片
					$newname = $this->uploadfile($file);//调用uploadfile方法，得到newname

					//上传新头像并且根据uid更新数据
					/*$arr=explode('.', $file['pic']['name']);
     			   	$suffix=$arr[count($arr)-1];
       				$newname=date("YmdHis").rand(100,999).'.'.$suffix;
        			$filepath = './photos/';
        			move_uploaded_file($_FILES['pic']['tmp_name'], $filepath.$newname);*///上传文件到指定文件夹，tmp_name是file里带的
        			$sql="update users set uname='$username',birthdate='$birthdate',gender=$gender,hobby='$hobby',degree=$degree,intro='$intro',pic='$newname' where uid=$uid";
				}
				   $result=$this->conn->query($sql);
        			return $result;

			//根据uid修改数据库中的数据

		}
}

  ?>
		